<?php
class Admin_AdminController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        $key = empty($p['key'])?'':Tool_Fnc::safe_string($p['key']);

        $tWhere = ' 1 and a.is_delete = 1';
        
        if (!empty($pDepartment)) {
           $tWhere .= ' and w.department = '.$pDepartment;
        }
        if(!empty($key)){
            $tWhere .= ' and w.name like \'%'.$key.'%\'';
        }

        $tAMO = new AdminModel;
        $tDIMO = new DepartmentinfoModel;
        $tRIMO = new RoleinfoModel;
        $tWMO = new WorkerModel;
        $pDeinfo = $tDIMO->field('num,introduce')->fList();
        $tSql = 'select count(*) c from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id left join '.$tDIMO->table.' di on di.num = w.department left join '.$tRIMO->table.' ri on ri.num = a.role where '.$tWhere;         
        $tCount = $tAMO->query($tSql);
        // print_r($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tSql = 'select a.id id,w.name name,di.introduce department,ri.introduce role,a.department departments,a.name username from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id left join '.$tDIMO->table.' di on di.num = w.department left join '.$tRIMO->table.' ri on ri.num = a.role where '.$tWhere.' order by a.id desc limit '.$tPage->limit();
            $tDatas = $tAMO->query($tSql);
            foreach($tDatas as $tKey=>$tRow){
                $tDatas[$tKey]['departments'] = explode(',',$tRow['departments']);
                foreach($tDatas[$tKey]['departments'] as $tK=>$tValue){
                    $tData = $tDIMO->field('introduce')->where('num = '.$tDatas[$tKey]['departments'][$tK])->fRow();
                    $tDatas[$tKey]['departments'][$tK] = $tData['introduce'];
                }
            }
        }
        // print_r($pDeinfo);
        
        //部门显示
        $tDIDatas = $tDIMO->field('*')->fList();

        $this->assign('pDepartment',$pDepartment);
        $this->assign('pKey',$key);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('tShow',$tPage->show());
        $this->assign('tDatas',$tDatas);
        $this->assign('pDeinfo',$pDeinfo);
    
    }
    public function addAction(){
        $p = $_REQUEST;
        $pDepartmentID = empty($p['department_id'])?'':Tool_Fnc::safe_string($p['department_id']);  
        $pRole = empty($p['role'])?'':Tool_Fnc::safe_string($p['role']);
        $pWid = empty($p['w_id'])?'':Tool_Fnc::safe_string($p['w_id']);

        $tWhere = ' 1 ';

        $tDIMO = new DepartmentinfoModel;
        $tRIMO = new RoleinfoModel;
        $tWMO = new WorkerModel;

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pWid = empty($p['w_id'])?Tool_Fnc::ajaxMsg('员工姓名没有选择'):Tool_Fnc::safe_string($p['w_id']);
            $pName = empty($p['name'])?Tool_Fnc::ajaxMsg('账号名称不能为空'):Tool_Fnc::safe_string($p['name']);
            $tTime = time();

            $tAMO = new AdminModel;
            $tRow = $tAMO->field('id')->where('name = '.$pName)->fRow();
            if(!empty(count($tRow))){Tool_Fnc::ajaxMsg('用户名已存在');}

            $pPasswd = empty($p['passwd'])?Tool_Fnc::ajaxMsg('密码不能为空'):trim($p['passwd']);
            
            $pRole = empty($p['role'])?Tool_Fnc::ajaxMsg('角色没有选择'):Tool_Fnc::safe_string($p['role']);
            $pDepartment = empty(rtrim(implode(',',$p['department']),','))?Tool_Fnc::ajaxMsg('管理部门没有分配'):Tool_Fnc::safe_string(rtrim(implode(',',$p['department']),','));
            $pPrivateKey = Tool_Fnc::newrand();
            $pPasswd = Tool_Fnc::markmd5($pPasswd,$pPrivateKey);
            $tData = array(
                'w_id' => $pWid,
                'name' => $pName,
                'passwd' => $pPasswd,
                'private_key' => $pPrivateKey,
                'role' => $pRole,
                'role_status' => 1,
                'department' => $pDepartment,
                'status' => 1,
                'created' => $tTime,
            );
            if(!$tAMO->insert($tData)){
                Tool_Fnc::ajaxMsg('添加失败');
            }
            Tool_Fnc::ajaxMsg('添加成功',1);
        }
        if(!empty($pRole)){
            //判断是不是超级管理员或者管理员
            if($pRole == 1 || $pRole == 2){
                $tDatas = $tDIMO->field('num,introduce')->fList(); 
            }else{
                $tSql = 'select di.num num,di.introduce introduce from '.$tWMO->table.' w left join '.$tDIMO->table.' di on di.num = w.department where w.id = '.$pWid;
                $tDatas = $tWMO->query($tSql);
            }
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
        if(!empty($pDepartmentID)){
            $tDatas = $tWMO->field('id,name')->where('department = '.$pDepartmentID.' and role_status = 2')->fList();
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
        //所在部门显示
        $tDDatas = $tDIMO->field('num,introduce')->fList();
        //所属角色显示
        $tRDatas = $tRIMO->field('num,introduce')->fList();
        
        $this->assign('tDDatas',$tDDatas);
        $this->assign('tRDatas',$tRDatas);
    
    }
    public function editAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);
        $pDepartmentID = empty($p['department_id'])?'':Tool_Fnc::safe_string($p['department_id']);  
        $pRole = empty($p['role'])?'':Tool_Fnc::safe_string($p['role']);
        $pWid = empty($p['w_id'])?'':Tool_Fnc::safe_string($p['w_id']);

        $tWhere = ' 1 ';

        $tDIMO = new DepartmentinfoModel;
        $tRIMO = new RoleinfoModel;
        $tWMO = new WorkerModel;
        $tAMO = new AdminModel;

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pTid = empty($p['tId'])?'':Tool_Fnc::safe_string($p['tId']);
            $pId = empty($p['id'])?'':Tool_Fnc::safe_string($p['id']);
            if(!empty($pId)){
                $tSql = 'select w.name name,w.department department,a.w_id w_id,a.role role,a.department departments,a.name username from '.$tAMO->table.' a left join '.$tWMO->table.' w on w.id = a.w_id where a.id = '.$pId;
                $tDatas = $tAMO->query($tSql);
                foreach($tDatas as $tKey=>$tRow){
                    $tDatas[$tKey]['departments'] = explode(',',$tRow['departments']);
                    foreach($tDatas[$tKey]['departments'] as $tK=>$tValue){
                        $tData = $tDIMO->field('introduce,num')->where('num = '.$tDatas[$tKey]['departments'][$tK])->fRow();
                        //unset($tDatas[$tKey]['departments'][$tK]);
                        //$tK = $tData['num'];
                        $tDatas[$tKey]['departments'][$tK] = $tData['introduce'];
                    }
                }
                Tool_Fnc::ajaxMsg('',1,$tDatas);
            } 
            $pWid = empty($p['w_id'])?Tool_Fnc::ajaxMsg('员工姓名没有选择'):Tool_Fnc::safe_string($p['w_id']);
            $pName = empty($p['name'])?Tool_Fnc::ajaxMsg('账号名称不能为空'):Tool_Fnc::safe_string($p['name']);
            $tTime = time();

            $tRow = $tAMO->field('id')->where('name = '.$pName)->fRow();
            if(empty(count($tRow))){Tool_Fnc::ajaxMsg('该用户不存在');}

            $pPasswd = empty($p['passwd'])?Tool_Fnc::ajaxMsg('密码不能为空'):trim($p['passwd']);
            
            //$pRole = empty($p['role'])?Tool_Fnc::ajaxMsg('角色没有选择'):Tool_Fnc::safe_string($p['role']);
            $pDepartment = empty(rtrim(implode(',',$p['department']),','))?Tool_Fnc::ajaxMsg('管理部门没有分配'):Tool_Fnc::safe_string(rtrim(implode(',',$p['department']),','));
            $pPrivateKey = Tool_Fnc::newrand();
            $pPasswd = Tool_Fnc::markmd5($pPasswd,$pPrivateKey);
            $tData = array(
                'w_id' => $pWid,
                'name' => $pName,
                'passwd' => $pPasswd,
                'private_key' => $pPrivateKey,
                //'role' => $pRole,
                'department' => $pDepartment,
                'status' => 1,
                'created' => $tTime,
                'id' => $pTid,
            );
            if(!$tAMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }
            Tool_Fnc::ajaxMsg('修改成功',1);
        }
        if(!empty($pRole)){
            //判断是不是超级管理员或者管理员
            if($pRole == 1 || $pRole == 2){
                $tDatas = $tDIMO->field('num,introduce')->fList(); 
            }else{
                $tSql = 'select di.num num,di.introduce introduce from '.$tWMO->table.' w left join '.$tDIMO->table.' di on di.num = w.department where w.id = '.$pWid;
                $tDatas = $tWMO->query($tSql);
            }
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
        if(!empty($pDepartmentID)){
            $tDatas = $tWMO->field('id,name')->where('department = '.$pDepartmentID.' and role_status = 2')->fList();
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
        //所在部门显示
        $tDDatas = $tDIMO->field('num,introduce')->fList();
        //所属角色显示
        $tRDatas = $tRIMO->field('num,introduce')->fList();

        $this->assign('pId',$pId);
        $this->assign('tDDatas',$tDDatas);
        $this->assign('tRDatas',$tRDatas);

    
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?die('ID不能为空'):intval($p['id']);

        $tAMO = new AdminModel;
        $tTime = time();
        $tData = array(
            'id' => $pId,
            'is_delete' => 2,
            'updated' => $tTime,
        );
        if(!$tAMO->update($tData)){
            Tool_Fnc::ajaxMsg('删除失败');
        }        
        Tool_Fnc::ajaxMsg('删除成功',1);
    }
}
?>
